package com.spring.sunflower.dialog

import android.content.Context
import android.text.TextUtils
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.core.CenterPopupView
import com.spring.sunflower.R
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.DoubleClickUtils

/**
 * Created by Kevin on 2021/8/6<br/>
 *
 * Describe:<br/>
 */
class EnvironmentChoiceDialog(
    context: Context,
    var listener: OnCreateViewListener?
) : CenterPopupView(context) {
    private var llContainer: LinearLayout? = null
    private var tvOK: TextView? = null
    private var tvCancel: TextView? = null
    private var tv: TextView? = null
    private var tvProduct: TextView? = null
    private var tvTest: TextView? = null
    private var tvLocalTest: TextView? = null
    private var etLocal: EditText? = null
    private var environmentSelected = Constant.ENVIRONMENT_PRODUCT
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_environment_choice
    }

    override fun onCreate() {
        super.onCreate()
        llContainer = findViewById<LinearLayout>(R.id.llContainer)
        tvOK = findViewById(R.id.tvOK)
        tvCancel = findViewById(R.id.tvCancel)
        tv = findViewById(R.id.tv)
        tvProduct = findViewById<TextView>(R.id.tvProduct)
        tvTest = findViewById<TextView>(R.id.tvTest)
        tvLocalTest = findViewById<TextView>(R.id.tvLocalTest)
        etLocal = findViewById<EditText>(R.id.etLocal)
        listener?.onCreateView()
        val environment =
            SPUtils.getInstance().getString(Constant.KEY_ENVIRONMENT, Constant.ENVIRONMENT_PRODUCT)
        if (environment.equals(Constant.ENVIRONMENT_PRODUCT)) {
            environmentSelected = Constant.ENVIRONMENT_PRODUCT
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
        } else if (environment.equals(Constant.ENVIRONMENT_TEST)) {
            environmentSelected = Constant.ENVIRONMENT_TEST
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
        } else if (environment.equals(Constant.ENVIRONMENT_LOCAL_TEST)) {//本地测试
            environmentSelected = Constant.ENVIRONMENT_LOCAL_TEST
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            etLocal?.visibility = VISIBLE
            val localEnvi: String =
                SPUtils.getInstance().getString(Constant.KEY_ENVIRONMENT_LOCAL, "")
            if (!TextUtils.isEmpty(localEnvi)) {
                etLocal?.setText(localEnvi)
            }
        }
        tvOK?.setOnClickListener {
            if (environmentSelected == Constant.ENVIRONMENT_LOCAL_TEST) {
                val site = etLocal?.text.toString().trim()
                if (TextUtils.isEmpty(site)) {
                    listener?.onClickOK(Constant.BASE_URL_LOCAL_TEST)
                    ToastUtils.showShort("使用默认:${Constant.BASE_URL_LOCAL_TEST}")
                } else {
                    listener?.onClickOK(site)
                }
            } else {
                listener?.onClickOK(environmentSelected)
            }
            dismiss()
        }
        tvCancel?.setOnClickListener {
            listener?.onClickCancel()
            dismiss()
        }
        tvProduct?.setOnClickListener {
            if (DoubleClickUtils.isFastShortClick()) return@setOnClickListener
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            environmentSelected = Constant.ENVIRONMENT_PRODUCT
            etLocal?.visibility = GONE
        }
        tvTest?.setOnClickListener {
            if (DoubleClickUtils.isFastShortClick()) return@setOnClickListener
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            environmentSelected = Constant.ENVIRONMENT_TEST
            etLocal?.visibility = GONE
        }
        tvLocalTest?.setOnClickListener {
            if (DoubleClickUtils.isFastShortClick()) return@setOnClickListener
            tvProduct?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_disable)
            tvLocalTest?.setBackgroundResource(R.drawable.ic_btn_bg_click_enable)
            environmentSelected = Constant.ENVIRONMENT_LOCAL_TEST
            etLocal?.visibility = VISIBLE
            val localEnvi: String =
                SPUtils.getInstance().getString(Constant.KEY_ENVIRONMENT_LOCAL, "")
            if (!TextUtils.isEmpty(localEnvi)) {
                etLocal?.setText(localEnvi)
            }
        }
    }

    fun getContainer(): LinearLayout {
        return llContainer!!
    }

//    private var xListener: OnCreateViewListener? = null
//    fun setOnCreateViewListener(l: OnCreateViewListener) {
//        xListener = l
//    }

    interface OnCreateViewListener {
        fun onCreateView()
        fun onClickOK(str:String)
        fun onClickCancel() {}
    }

    fun setOKText(str: String) {
        if (str.isNullOrEmpty()) return
        tvOK?.text = str
        invalidate()
    }

    fun setCancelText(str: String) {
        if (str.isNullOrEmpty()) return
        tvCancel?.text = str
    }

    fun setHeadTitle(str: String) {
        if (str.isNullOrEmpty()) return
        tv?.text = str
        invalidate()
    }

    fun setTitleText(str: String) {
        if (str.isNullOrEmpty()) return
        tvProduct?.text = str
    }

    fun setTitleColor(@ColorInt color: Int) {
        tvProduct?.setTextColor(color)
    }

    fun setDescText(str: String) {
        if (str.isNullOrEmpty()) return
        tvTest?.text = str
    }

    fun setDescColor(@ColorInt color: Int) {
        tvTest?.setTextColor(color)
    }
}